package com.buding.fx.utils;


import cn.hutool.json.JSONObject;

import org.apache.commons.lang3.StringUtils;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author GuoLiangYan
 * @Date 9:38 2019/8/27
 */
public class ResultMap {
    private Integer retCode = null;
    private Object retData = null;
    private String retMsg = null;
    private boolean isEmpty = false;
    private boolean check = false;

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }
    public boolean getDataCheck(){
        return check;
    }

    private static final ThreadLocal<ResultMap> localMsg = ThreadLocal.withInitial(() -> new ResultMap());

    public enum Mcode {
        ERROR(-1, "系统错误"),
        SUCCESS(200, "请求成功"),
        REDIRECT(300, "重定向"),
        BADREQUEST(400, "请求数据不合法"),
        UNAVAILABLE(500, "服务不可用");

        /**
         * 消息码
         */
        private Integer code;
        /**
         * 消息描述
         */
        private String desc;

        Mcode(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public Integer getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }
    }



    public static HashMap<String, Object> success() {
        if (template().check) {
            return template().returnMap();
        }
        return template(Mcode.SUCCESS).returnMap();
    }

    public static HashMap<String, Object> success(Object object) {
        if (template().check) {
            return template().returnMap();
        }
        return template(Mcode.SUCCESS).verify(object).returnMap();
    }





    public static HashMap<String, Object> error() {
        return template(Mcode.ERROR).returnMap();
    }

    public static HashMap<String, Object> error(Object object) {
        if (template().check) {
            return template().returnMap();
        }
        return template(Mcode.ERROR).verify(object).returnMap();
    }



    /**
     * 自定义消息
     *
     * @return object
     */
    public static ResultMap custom() {
        return template().reset();
    }

    public static ResultMap custom(Object object) {
        return custom().verify(object);
    }

    /**
     * 自定义消息code
     *
     * @param retCode
     * @return
     */
    public ResultMap setRetCode(Integer retCode) {
        if (retCode != null) {
            this.retCode = retCode;
        }
        return this;
    }

    /**
     * 自定义消息文本
     *
     * @param retMsg
     * @return
     */
    public ResultMap setRetMsg(String retMsg) {
        if (retMsg != null) {
            this.retMsg = retMsg;
        }
        return this;
    }
    public ResultMap setRetData(String retData) {
        if (retData != null) {
            this.retData = retData;
        }
        return this;
    }

    public static boolean getCheck() {
        return template().check;
    }

    /**
     * 格式化消息
     *
     * @return {"retData"："测试","retCode":0,"retMsg"："请求成功"}
     */
    public HashMap<String, Object> returnMap() {
//        if (this.isEmpty) {
//            template(ResultMap.Mcode.EMPTY);
//        }

        HashMap<String, Object> obj = new HashMap<>(8);
        obj.put("code", this.retCode);
        obj.put("msg", this.retMsg);
        if (this.retData != null) {
            obj.put("data", this.retData);
        } else {
            obj.put("data", new ArrayList<>());
        }
        localMsg.remove();
        return obj;
    }

    public HashMap<String, Object> returnDataMap() {
        HashMap<String, Object> obj = new HashMap<>(8);
        obj.put("code", this.retCode);
        obj.put("msg", this.retData);
        if (this.retData != null) {
            obj.put("data", this.retData);
        } else {
            obj.put("data", new ArrayList<>());
        }
        localMsg.remove();
        return obj;
    }


    private static ResultMap template() {
        return localMsg.get();
    }

    private static ResultMap template(Mcode mc) {
        return template().reset().setRetCode(mc.getCode()).setRetMsg(mc.getDesc());
    }

    private ResultMap verify(Object object) {
        boolean isNull = false;
        if (object == null) {
            isNull = true;
        } else if (String.class.isInstance(object)) {
            String string = (String) object;
            if (string == null || StringUtils.isBlank(string)) {
                isNull = true;
            }
        } else if (List.class.isInstance(object)) {
            List list = (List) object;
            if (list == null || list.size() <= 0) {
                isNull = true;
            }
        } else if (JSONObject.class.isInstance(object)) {
            JSONObject json = (JSONObject) object;
            if (json == null || json.isEmpty()) {
                isNull = true;
            }
        } else if (Map.class.isInstance(object)) {
            Map map = (Map) object;
            if (map == null || map.isEmpty()) {
                isNull = true;
            }
        }
        if (!isNull) {
            this.retData = object;
        }
        this.isEmpty = isNull;
        return this;
    }

    private ResultMap reset() {
        this.retCode = null;
        this.retMsg = null;
        this.retData = null;
        this.isEmpty = false;
        return this;
    }

}
